﻿Imports System.Data.OleDb

Public Class Reputation
    Protected UserName As String
    Protected Score As Integer
    Protected Const PromoteScore As Integer = 100
    Protected Const PromoteRole As String = "Editor"

    Protected ReadOnly Property FishTankConnectionString() As String
        Get
            Return ConfigurationManager.ConnectionStrings("fishTankConnection").ConnectionString
        End Get
    End Property

    Public Sub New(ByVal UserName As String)
        Me.UserName = UserName
        QueryScore()
    End Sub

    Public Function PromoteUser() As Boolean
        If Not Roles.IsUserInRole(UserName, PromoteRole) AndAlso Score > PromoteScore Then
            Roles.AddUserToRole(UserName, PromoteRole)
            Return True
        Else
            Return False
        End If
    End Function

    Protected Sub QueryScore()
        Dim sqlQuery As String = "SELECT ReputationScore FROM SiteUserReputation " &
                                 "WHERE UserName=@UserName"
        Using connection As New OleDbConnection(FishTankConnectionString)
            Using command As New OleDbCommand(sqlQuery, connection)
                command.CommandType = CommandType.Text
                command.Parameters.AddWithValue("@UserName", UserName)
                connection.Open()
                command.ExecuteNonQuery()
                Score = CInt(command.ExecuteScalar())
            End Using
        End Using
    End Sub

End Class
